﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GiaPhuc.Data;
using GiaPhuc.Model;

namespace GiaPhuc.Pages
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        private int ProductID
        {
            get
            {
                if (null != Request["ID"])
                {
                    return int.Parse(Request["ID"]);
                }
                return 0;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Helper.ConnectionString))
                {
                    using (ResellEraDMInPhUkIeNipAdX context = new ResellEraDMInPhUkIeNipAdX(connection))
                    {

                        int productID = ProductID;
                        Products product = context.Products.SingleOrDefault(x => x.ProductsID == productID);

                        if (null != product)
                        {

                            this.lblPrice.Text = product.ProductsPrice.ToString("#,##0.##");
                            this.ProductImage.ImageUrl = product.ProductsImage;
                            this.linkProductImage.NavigateUrl = product.ProductsImage;

                            ProductsDescription description = context.ProductsDescription.SingleOrDefault(x => x.ProductsID == productID);

                            this.Title = "CỬA HÀNG TINH TẾ (NICE STORE) - PHỤ KIỆN IPAD - " + description.ProductsName;
                            this.lblProductName.Text = description.ProductsName;
                            this.linkProductImage.ToolTip = description.ProductsName;
                            this.divProductImages.InnerHtml = description.ProductsImages.RemoveBadCode();
                            this.divProductInstruction.InnerHtml = description.ProductsDescription1.RemoveBadCode();

                            this.hypBuy.NavigateUrl = ("/Pages/CartPage.aspx?ProductID=" + product.ProductsID).ContextualURL(description.ProductsName);


                            IList<ProductModel> products = null;
                            // Load the product in the same category
                            var queryProducts = from x in context.Products.ToList()
                                                join y in context.ProductsDescription
                                                on x.ProductsID equals y.ProductsID
                                                where x.MasterCategoriesID == product.MasterCategoriesID
                                                select new { x, y };
                            if (null != queryProducts && queryProducts.Count() > 0)
                            {
                                products = new List<ProductModel>();
                                foreach (var item in queryProducts)
                                {
                                    products.Add(new ProductModel
                                    {
                                        ProductID = item.x.ProductsID,
                                        ProductName = item.y.ProductsName,
                                        Price = item.x.ProductsPrice,
                                        Specification = item.y.ProductsDescription1,
                                        ImagePath = item.x.ProductsImage,
                                        Url = ("/Pages/ProductDetail.aspx?ID=" + item.x.ProductsID).ContextualURL(item.y.ProductsName),
                                        CartUrl = ("/Pages/CartPage.aspx?ProductID=" + item.x.ProductsID).ContextualURL(item.y.ProductsName)
                                    });
                                }
                            }

                            dtlProducts.DataSource = products;
                            dtlProducts.DataBind();
                        }
                    }
                }
            }
        }
    }
}